#!/bin/sh

app_path=$0

# Need this for daisy-chained symlinks.
while
    APP_HOME=${app_path%"${app_path##*/}"}  # leaves a trailing /; empty if no leading path
    [ -h "$app_path" ]
do
    ls=$( ls -ld "$app_path" )
    link=${ls#*' -> '}
    case $link in             #(
      /*)   app_path=$link ;; #(
      *)    app_path=$APP_HOME$link ;;
    esac
done

# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

qemu-system-riscv64 \
    -machine virt \
    -nographic \
    -bios ${APP_HOME}/../../bootloader/rustsbi-qemu.bin \
    -device loader,file=${APP_HOME}/../target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000